import Vue from "vue";

const token_name = "ht.managerment.system.jwt";
/**
 * 获取登录用户
 */
async function getlogin() {
  if (Vue.$http.defaults.baseURL == "") {
    return null;
  }
  try {
    const res = await Vue.$http.get("/login/getLoginUser");
    if (res.status == 200) {
      const rt = res.data;
      if (rt.errorCode == "SUCCESS") {
        return rt.returnData;
      }
      return null;
    } else {
      console.log(res);
      return null;
    }
  } catch (error) {
    console.log(error);
    return null;
  }
}

function islogin() {
  var token = getauthorization();
  return token != null && token != "" && token != "empty";
}

function getauthorization() {
  var token = Vue.$cookies.get(token_name);
  if (Vue.$_.isEmpty(token)) {
    return "empty";
  }
  return token;
}

function signOut() {
  Vue.$cookies.remove(token_name);
}

function signCookie(authorization) {
  //Vue.$cookies.remove(token_name);
  Vue.$cookies.set(token_name, authorization);
}

/**
 *
 * @param {import("vue").VueConstructor} Vue
 */
export default function(_Vue) {
  _Vue.prototype.$getlogin = getlogin;
  _Vue.$getlogin = getlogin;

  _Vue.prototype.$signOut = signOut;
  _Vue.$signOut = signOut;

  _Vue.prototype.$authorization = getauthorization;
  _Vue.$authorization = getauthorization;

  _Vue.prototype.$islogin = islogin;
  _Vue.$islogin = islogin;

  _Vue.prototype.$signCookie = signCookie;
  _Vue.$signCookie = signCookie;
}
